/*
 * driver page, view source to see form/post info http://aa.usno.navy.mil/data/docs/RS_OneYear.php
 * page that gets posted to http://aa.usno.navy.mil/cgi-bin/aa_rstablew.pl
 */

var tgd = tgd || {};
tgd.Services = tgd.Services || {};
tgd.Services.USNO = tgd.Services.USNO || {};

tgd.Services.USNO.SunriseSunset = function (year, type, state, place, callback) {
	this._year = year;
	this._type = type;
	this._state = state;
	this._place = place;
	this._callback = callback;

	this._data = {};  // { Months : [ { Name, Id, Days : [ { Date, Sunrise : { Hour, Minute, toString }, Sunset : { Hour, Minute, toString } } ] } ] }

	if (tgd.Services.USNO.SunriseSunset._initialized === undefined) {

		tgd.Services.USNO.SunriseSunset.prototype._TODO = function() {
			if (this._request.readyState == 4) {
				this._data = eval('(' + this._request.responseText + ')');
				this._callback();
			}
		}

		tgd.Services.USNO.SunriseSunset.prototype.refresh = function() {
			this._request.open("POST", "http://aa.usno.navy.mil/cgi-bin/aa_rstablew.pl", true);
			this._request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			this._request.send("FFX=1&xxy=" + this._year + "&type=" + this._type + "&st=" + this._state + "&place=" + this._place + "&ZZZ=END");
		}

		tgd.Services.USNO.SunriseSunset.prototype.getData = function() {
			return this._data;
		}

		tgd.Services.USNO.SunriseSunset._initialized = true;
	}

	var self = this;
	this._request = new XMLHttpRequest();
	this._request.onreadystatechange = function() {
		if (self._request.readyState == 4) {
			var preData = pkb._request.responseText.substr(pkb._request.responseText.indexOf("<pre>"), 
							pkb._request.responseText.indexOf("</pre>") - 
							pkb._request.responseText.indexOf("<pre>"))
							.split("\n")

			if (preData.length < 40) {  //data was not returned
				alert("Input was invalid, no data returned.");
				return;
			}

			self._data.Months = [];
			for (var i = 0; i < 12; self._data.Months[i] = { Name : tgd.Utilities.Calendar.MonthNames[i], Id : i++, Days : [] });

			for (var i = 0; i < 31; ++i) {
				var day = parseInt(preData[i + 10].substr(0,2), 10);
				var times = preData[i + 10 /* first 10 rows are junk */].substr(4);

				for (var j = 0; j < 12; ++j) {
					if (times.substr(j * 11, 4) != "    ") {  //don't add empty "Days"
						self._data.Months[j].Days[day - 1] = {};
						self._data.Months[j].Days[day - 1].Date = day;

						self._data.Months[j].Days[day - 1].Sunrise = {};
						self._data.Months[j].Days[day - 1].Sunrise.Hour = parseInt(times.substr(j * 11, 2), 10);
						self._data.Months[j].Days[day - 1].Sunrise.Minute = parseInt(times.substr(j * 11 + 2, 2), 10);
						self._data.Months[j].Days[day - 1].Sunrise.toString = function() { return this.Hour.toString() + ":" + (this.Minute < 10 ? "0" : "") + this.Minute.toString(); };

						//todo: try Days[xxx].Sunset = {Hour : 3, Minute :4, toSTring : ...};
						self._data.Months[j].Days[day - 1].Sunset = {};
						self._data.Months[j].Days[day - 1].Sunset.Hour = parseInt(times.substr(j * 11 + 5, 2), 10);
						self._data.Months[j].Days[day - 1].Sunset.Minute = parseInt(times.substr(j * 11 + 7, 2), 10);
						self._data.Months[j].Days[day - 1].Sunset.toString = function() { return this.Hour.toString() + ":" + (this.Minute < 10 ? "0" : "") + this.Minute.toString(); };

						//todo: format this mess
						self._data.Months[j].Days[day - 1].SunlightPercentage = (
							(self._data.Months[j].Days[day - 1].Sunset.Hour + (self._data.Months[j].Days[day - 1].Sunset.Minute / 60))
							-
							(self._data.Months[j].Days[day - 1].Sunrise.Hour + (self._data.Months[j].Days[day - 1].Sunrise.Minute / 60))) / 24 * 100;
					}
				}
			}

			self._callback();
		}
	};

	this.refresh();
}
